﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Windows.Forms;

using TLL.Repository;
using TLL.Finance.Currencys;
using TLL.Debug;

namespace TLL.Finance
{
    public class RUSCurrencyRepository : XMLRepository<Currency>
    {
        public RUSCurrencyRepository(NotifyIcon notify) : base("http://www.cbr.ru/scripts/XML_daily.asp?date_req=" + DateTime.Now.ToShortDateString().Replace('.', '/'), "Debug\\Log.txt", false, true, XMLDirectionType.Web, notify) { }

        public override List<Currency> Get(int id = 0)
        {
            try
            {
                var res = new List<Currency>();

                foreach (XmlNode valute in this.Document.ChildNodes[1].ChildNodes)
                    switch (this.FindNode(valute, "CharCode").InnerText)
                    {
                        case "USD":
                            res.Add(new Currency(
                                CurrencyType.USD,
                                CurrencyCountry.Russian,
                                double.Parse(this.FindNode(valute, "Value").InnerText),
                                double.Parse(this.FindNode(valute, "Value").InnerText),
                                double.Parse(this.FindNode(valute, "Value").InnerText),
                                double.Parse(this.FindNode(valute, "Value").InnerText),
                                double.Parse(this.FindNode(valute, "Value").InnerText),
                                double.Parse(this.FindNode(valute, "Value").InnerText)
                                ));
                            break;
                        case "EUR":
                            res.Add(new Currency(
                                CurrencyType.EUR,
                                CurrencyCountry.Russian,
                                double.Parse(this.FindNode(valute, "Value").InnerText),
                                double.Parse(this.FindNode(valute, "Value").InnerText),
                                double.Parse(this.FindNode(valute, "Value").InnerText),
                                double.Parse(this.FindNode(valute, "Value").InnerText),
                                double.Parse(this.FindNode(valute, "Value").InnerText),
                                double.Parse(this.FindNode(valute, "Value").InnerText)
                                ));
                            break;
                    }
                return res;
            }
            catch (Exception ex) 
            {
                this.Error = new Error(ErrorType.Get);
                this.Warning = new Warning(
                    WarningType.Internet,
                    string.Format("Error getting rus currencies"),
                    this.Notify, 
                    delegate(object sender, EventArgs e) {
                        this.Error.Show(DisplayDebugType.MessageBox);
                    });
            }
            return null;
        }
    }
}
